import 'package:flutter/material.dart' hide SearchController;

import '../../utils/util.dart';
import 'index.dart';
import 'widgets/widgets.dart';

class SearchPage extends GetView<SearchController> {
  const SearchPage({Key? key}) : super(key: key);

  // 主视图
  Widget _buildView() {
    var state = controller.state;
    return !state.isSearching ? SearchHistoryView() : SearchResultView();
  }

  @override
  Widget build(BuildContext context) {
    var state = controller.state;
    return GetBuilder<SearchController>(
      builder: (_) {
        return Hero(
          tag: 'search',
          child: Scaffold(
            appBar: AppBar(
              flexibleSpace: SafeArea(
                child: Row(
                  children: [
                    IconButton(
                      onPressed: Get.back,
                      icon: Icon(
                        IconUtil.arrow_left2,
                        color: Colors.white,
                        size: 20.r,
                      ),
                    ),
                    Expanded(
                      child: Container(
                        height: 40.r,
                        padding: EdgeInsets.only(right: 10.r),
                        child: UITextField(
                          data: state.searchText,
                          onChanged: (value) {
                            state.searchText = value;
                            state.isSearching = state.searchText.isNotEmpty;
                            controller.update();
                          },
                          hintText: '小微企业',
                          prefixIcon: IconUtil.search,
                          suffixText: '搜索',
                          onSuffixTap: (_) => controller.actionSearch,
                          style: TextFieldStyle.outLine,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              bottom: PreferredSize(
                  preferredSize: Size.fromHeight(50.r),
                  child: TPTabBar(
                    controller: state.tabCtrl,
                  )),
            ),
            body: SafeArea(
              child: _buildView(),
            ),
          ),
        );
      },
    );
  }
}
